/*
Copyright 2017 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// This file was autogenerated by apiregister-gen. Do not edit it manually!

package example

import (
	"context"
	"fmt"

	"k8s.io/apimachinery/pkg/apis/meta/internalversion"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/runtime/schema"
	"k8s.io/apiserver/pkg/registry/rest"
	"sigs.k8s.io/apiserver-builder-alpha/pkg/builders"
)

var (
	ExamplePodStorage = builders.NewApiResource( // Resource status endpoint
		InternalPod,
		func() runtime.Object { return &Pod{} },     // Register versioned resource
		func() runtime.Object { return &PodList{} }, // Register versioned resource list
		&PodStrategy{builders.StorageStrategySingleton},
	)
	InternalPod = builders.NewInternalResource(
		"pods",
		"Pod",
		func() runtime.Object { return &Pod{} },
		func() runtime.Object { return &PodList{} },
	)
	InternalPodStatus = builders.NewInternalResourceStatus(
		"pods",
		"PodStatus",
		func() runtime.Object { return &Pod{} },
		func() runtime.Object { return &PodList{} },
	)
	// Registered resources and subresources
	ApiVersion = builders.NewApiGroup("example.pingcap.com").WithKinds(
		InternalPod,
		InternalPodStatus,
	)

	// Required by code generated by go2idl
	AddToScheme = (&runtime.SchemeBuilder{
		ApiVersion.SchemeBuilder.AddToScheme,
		RegisterDefaults,
	}).AddToScheme
	SchemeBuilder      = ApiVersion.SchemeBuilder
	localSchemeBuilder = &SchemeBuilder
	SchemeGroupVersion = ApiVersion.GroupVersion
)

// Required by code generated by go2idl
// Kind takes an unqualified kind and returns a Group qualified GroupKind
func Kind(kind string) schema.GroupKind {
	return SchemeGroupVersion.WithKind(kind).GroupKind()
}

// Required by code generated by go2idl
// Resource takes an unqualified resource and returns a Group qualified GroupResource
func Resource(resource string) schema.GroupResource {
	return SchemeGroupVersion.WithResource(resource).GroupResource()
}

// +genclient
// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

type Pod struct {
	metav1.TypeMeta
	metav1.ObjectMeta
	Spec   PodSpec
	Status PodStatus
}

type PodStatus struct {
	Phase string
}

//
// Pod Functions and Structs
//
// +k8s:deepcopy-gen=false
type PodStrategy struct {
	builders.DefaultStorageStrategy
}

// +k8s:deepcopy-gen=false
type PodStatusStrategy struct {
	builders.DefaultStatusStorageStrategy
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

type PodList struct {
	metav1.TypeMeta
	metav1.ListMeta
	Items []Pod
}

func (Pod) NewStatus() interface{} {
	return PodStatus{}
}

func (pc *Pod) GetStatus() interface{} {
	return pc.Status
}

func (pc *Pod) SetStatus(s interface{}) {
	pc.Status = s.(PodStatus)
}

func (pc *Pod) GetSpec() interface{} {
	return pc.Spec
}

func (pc *Pod) SetSpec(s interface{}) {
	pc.Spec = s.(PodSpec)
}

func (pc *Pod) GetObjectMeta() *metav1.ObjectMeta {
	return &pc.ObjectMeta
}

func (pc *Pod) SetGeneration(generation int64) {
	pc.ObjectMeta.Generation = generation
}

func (pc Pod) GetGeneration() int64 {
	return pc.ObjectMeta.Generation
}

// Registry is an interface for things that know how to store Pod.
// +k8s:deepcopy-gen=false
type PodRegistry interface {
	ListPods(ctx context.Context, options *internalversion.ListOptions) (*PodList, error)
	GetPod(ctx context.Context, id string, options *metav1.GetOptions) (*Pod, error)
	CreatePod(ctx context.Context, id *Pod) (*Pod, error)
	UpdatePod(ctx context.Context, id *Pod) (*Pod, error)
	DeletePod(ctx context.Context, id string) (bool, error)
}

// NewRegistry returns a new Registry interface for the given Storage. Any mismatched types will panic.
func NewPodRegistry(sp builders.StandardStorageProvider) PodRegistry {
	return &storagePod{sp}
}

// Implement Registry
// storage puts strong typing around storage calls
// +k8s:deepcopy-gen=false
type storagePod struct {
	builders.StandardStorageProvider
}

func (s *storagePod) ListPods(ctx context.Context, options *internalversion.ListOptions) (*PodList, error) {
	if options != nil && options.FieldSelector != nil && !options.FieldSelector.Empty() {
		return nil, fmt.Errorf("field selector not supported yet")
	}
	st := s.GetStandardStorage()
	obj, err := st.List(ctx, options)
	if err != nil {
		return nil, err
	}
	return obj.(*PodList), err
}

func (s *storagePod) GetPod(ctx context.Context, id string, options *metav1.GetOptions) (*Pod, error) {
	st := s.GetStandardStorage()
	obj, err := st.Get(ctx, id, options)
	if err != nil {
		return nil, err
	}
	return obj.(*Pod), nil
}

func (s *storagePod) CreatePod(ctx context.Context, object *Pod) (*Pod, error) {
	st := s.GetStandardStorage()
	obj, err := st.Create(ctx, object, nil, &metav1.CreateOptions{})
	if err != nil {
		return nil, err
	}
	return obj.(*Pod), nil
}

func (s *storagePod) UpdatePod(ctx context.Context, object *Pod) (*Pod, error) {
	st := s.GetStandardStorage()
	obj, _, err := st.Update(ctx, object.Name, rest.DefaultUpdatedObjectInfo(object), nil, nil, false, &metav1.UpdateOptions{})
	if err != nil {
		return nil, err
	}
	return obj.(*Pod), nil
}

func (s *storagePod) DeletePod(ctx context.Context, id string) (bool, error) {
	st := s.GetStandardStorage()
	_, sync, err := st.Delete(ctx, id, nil, &metav1.DeleteOptions{})
	return sync, err
}
